<template>
  <div id="header">
    <div class="logo-info">
      <img src="~@/assets/img/header/logo.svg" alt="">
      <span>网抑云音乐</span>
    </div>
    <div class="go-info">
      <span @click="goBack"><</span>
      <span @click="goAdd">></span>
    </div>
    <div class="search-info">
      <img src="~@/assets/img/header/search.svg" @click="imgSearchBtn" style="cursor:pointer;" alt="">
      <input type="text" v-model="searchWord" @focus="onfocus" @keydown="inputSearchBtn" @blur="onblur" placeholder="搜索">
      <div><img src="~@/assets/img/header/sing.svg" alt=""></div>
    </div>
<!--    登录区域-->
    <div class="login-info">
      <img  v-if="currentUserInfo === null" src="~@/assets/img/header/person.svg" alt="">
      <img v-else :src="currentUserInfo.avatarUrl" style="width: 35px;height: 35px;border-radius: 50%">

      <span v-if="currentUserInfo === null" @click="handleLogin">登录</span>
      <span v-else>{{currentUserInfo.nickname}}</span>
      <span v-if="currentUserInfo !== null" @click="logout">&nbsp;&nbsp;退出</span>
    </div>
<!--    登录界面弹出-->
    <el-drawer
      :modal="false"
      title="登录界面"
      style="margin-top: 44px;margin-bottom: 50px"
      :visible.sync="loginDialogVisible"
      @close="handleLoginDialogClosed">
      <div>
        <el-form :model="loginInfo" :rules="loginInfoRules" ref="loginInfoRuleForm" style="margin-top: 25px;">

          <el-form-item prop="phone">
            <el-input style="display: block;margin:100px auto 0;width: 60%" prefix-icon="el-icon-user"
                      v-model="loginInfo.phone" placeholder="请输入手机号"></el-input>

          </el-form-item>
          <el-form-item prop="password">
            <el-input style="display: block;margin:0 auto;width: 60%" show-password
                      prefix-icon="el-icon-lock" v-model="loginInfo.password" type="password"
                      placeholder="请输入密码"></el-input>
          </el-form-item>
        </el-form>
<!--        登录-->
        <el-button type="danger"
                   @click="loginMusic"
                   style="background-color: red;width: 60%;display: block;margin: 0 auto">登录
        </el-button>
      </div>

    </el-drawer>
<!--    搜索框-->
    <el-popover
      placement="bottom"
      width="350"
      v-model="visible">
      <p style="font-size: 15px;padding-bottom: 4px">热搜榜</p>
      <div v-for="(item, index) in hotList"
           class="hot_item"
           :class="{active:index <= 2}"
           @click="hotListItemClick(item)"
           :key="index" >
        <div class="num">{{index + 1}}</div>
        <div>
          <div class="search_key">{{item.searchWord}}</div>
          <div class="search_content">{{item.content}}</div>
        </div>
      </div>
    </el-popover>
  </div>
</template>

<script>
  import {home} from "@/network/home";

  export default {
    name: "Header",
    data(){
      return{
        visible:false,  //搜索框是否显示
        hotList:[],     //热搜列表
        searchWord:'',  //输入框绑定的数据
        loginDialogVisible: false,  //展示登录的对话框
        //登录的信息
        loginInfo: {
          phone: '',
          password: ''
        },
        //登录的校验规则
        loginInfoRules: {
          phone: [
            {required: true, message: ' ', trigger: 'blur'}
          ],
          password: [
            {required: true, message: ' ', trigger: 'blur'}
          ]
        },
        //当前用户的用户信息(已修改,保存在vuex)
        // currentUserInfo: window.localStorage.getItem('currentUserInfo') === 'null' ? null : JSON.parse(window.localStorage.getItem('currentUserInfo')),
      }
    },
    computed:{
      //当前用户信息
      currentUserInfo(){
        return this.$store.state.userInfo
      }
    },
    created() {
      //获取热搜数据
      this.getHotSearch()
    },
    watch:{
      '$store.state.userInfo'(){
        this.getLoginStatus()
        home.getTest().then(res => {
          console.log(res);
        })
      },
    },
    methods:{
      //路由前进、后退
      goAdd(){
        this.$router.go(1)
      },
      goBack(){
        this.$router.go(-1)
      },
      //鼠标聚焦出现搜索框
      onfocus(){
        this.visible = true
      },
      //鼠标失去聚焦搜索框消失
      onblur(){
        this.visible = false
      },
      //搜索（点列搜索框里的内容搜索）
      hotListItemClick(item){
        this.searchWord = item.searchWord
        this.$router.push('/search/searchsong/' + this.searchWord)
      },
      //搜索(自己输入搜索)
      inputSearchBtn(e){
        if(e.keyCode === 13 && this.searchWord !== ''){
          this.$router.push('/search/searchsong/' + this.searchWord)
        }
      },
      imgSearchBtn(){
        if(this.searchWord !== ''){
          this.$router.push('/search/searchsong/' + this.searchWord)
        }
      },

      // 登录
      //重设登录框的表单信息
      handleLoginDialogClosed() {
        this.$refs.loginInfoRuleForm.resetFields();
      },
      //顶部登录按钮
      handleLogin() {
        this.loginDialogVisible = true
      },
      //点击登录
      loginMusic() {
        this.$refs.loginInfoRuleForm.validate(valid => {
          if (!valid) return this.$message.error('请填写正确的信息')
          this.getLogin(this.loginInfo.phone,this.loginInfo.password)
          //隐藏弹窗
          this.loginDialogVisible = false
        })
      },
      //退出登录
      logout() {
        home.getLogout().then(res => {
          //告诉vuex退出当前用户
          this.$store.commit('logout')
          //存储用户信息的localstorage设置为null
          window.localStorage.setItem('currentUserInfo', null)
        })
      },


      //请求热搜数据的函数
      getHotSearch(){
        home.getHotSearch().then(res => {
          this.hotList = res.data.data
        })
      },
      //请求登录数据
      getLogin(phone, password){
        home.getLogin(phone, password).then(res => {
          console.log(res);
          if (res.status !== 200) console.log('登陆失败,请检查登录信息!')
          //保存cookie的信息
          window.localStorage.setItem('musicCookie', res.data.cookie)
          //保存用户信息,退出时删除
          window.localStorage.setItem('currentUserInfo', JSON.stringify(res.data.profile))
          //刷新页面
          this.$router.go(0)
          //将用户信息保存到vuex
          this.$store.commit('sendUserInfo', res.data.profile)
        })
      },
    }
  }
</script>

<style scoped>
  #header{
    /*position: fixed;*/
    width: 100%;
    height: 44px;
    background-color: #e53434;
    padding: 8px 20px 5px;
    display: flex;
    color: white;
    box-shadow:0 1px 2px rgba(0,0,0,.3);
    /*z-index: 99;*/
  }
  .logo-info{
    width: 200px;
    height: 100%;
  }
  .logo-info img{
    width: 28px;
    height: 28px;
  }
  .logo-info span{
  }
  .go-info{
    width: 100px;
    margin-top: 3px;
  }
  .go-info span{
    padding: 2px 6px;
    border-radius: 50%;
    cursor: pointer;
    font-size: 20px;
    background-color: rgba(0,0,0,.1);
  }
  .search-info{
    flex: 1;
    height: 100%;
    display: flex;
    justify-items: center;
  }
  /*第一代img*/
  .search-info>img{
    position: relative;
    left: 30px;
    top: 6px;
    width: 18px;
    height: 18px;
  }
  .search-info div{
    margin-left: 10px;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background-color: rgba(0,0,0,.1);
  }
  .search-info div img{
    position: relative;
    left: 5px;
    top: 3px;
    width: 23px;
    height: 23px;
  }
  /*改变搜索框placeholder的颜色*/
  .search-info input::placeholder{
    color: #FFFFFF;
  }
  .search-info input{
    outline: none;
    width: 160px;
    padding: 5px 32px;
    font-size: 12px;
    border-radius: 12px;
    color: white;
    border: 1px solid transparent;
    background-color: rgba(0,0,0,.1);

  }
  .login-info{
    width: 200px;
    margin-right: 20px;
  }
  .login-info span{
    font-size: 14px;
  }

  span,img{
    vertical-align: middle;
  }

  /*搜索框*/
  /deep/ .el-popover{
    position: fixed;
    left: 20%;
    top:38px;
    height: 480px;
    overflow-y: scroll;
  }
  /*隐藏滚动条*/
  /deep/ .el-popover::-webkit-scrollbar {
    display: none;
  }
  .hot_item{
    display: flex;
    padding: 8px 7px;
    cursor: pointer;
  }
  .hot_item:hover{
    background-color: rgba(0,0,0,.1);
  }
  .num{
    font-size: 15px;
    width: 30px;
    color: #BBBBBB;
    margin-top: 2px;
  }
  .search_key{
    font-size: 14px;
    color: #2a2727;
  }
  .search_content{
    font-size: 13px;
    color: #a8a5a5;
  }
  .active .num{
    color: #ff5777;
  }
  .active .search_key{
    font-weight: 700;
  }
</style>
